<template>
  <div class="app-container">
    <el-form label-width="120px">
      <el-form-item label="医院名称">
        <el-input v-model="hospset.hosname"/>
      </el-form-item>
      <el-form-item label="医院编号">
        <el-input v-model="hospset.hoscode"/>
      </el-form-item>
      <el-form-item label="api地址">
        <el-input v-model="hospset.apiUrl"/>
      </el-form-item>
      <el-form-item label="联系人">
        <el-input v-model="hospset.contactsName"/>
      </el-form-item>
      <el-form-item label="电话">
        <el-input v-model="hospset.contactsPhone"/>
      </el-form-item>
      <el-form-item>
        <el-button :disabled="saveBtnDisabled" type="primary" @click="saveOrUpdate">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import hospset from '@/api/hospSet'//引入api模块
export default {
  data() {
    return {
      hospset: {},
      saveBtnDisabled: false // 保存按钮是否禁用,
    }
  },
  //注意：获取路由参数，使用的是 $route ，没有r     
created(){
    if(this.$route.params && this.$route.params.id){
        const id = this.$route.params.id
        this.fetchDataById(id)
    }
},
  methods: {
    saveOrUpdate() {
      this.saveBtnDisabled = true
      this.saveData()
    },
    // 保存
    saveData() {
          hospset.save(this.hospset).then(response=>{
        this.$message({type:'success',message:'保存成功'})
        //注意：路由跳转时 使用 $router ,有r
        this.$router.push({
            path:'/hospSet/list'  
        })
    })
    },
    
    //根据id查询数据
    fetchDataById(id){
        hospset.getById(id).then(response=>{
          //将根据id查询的数据【json对象】传递给hospSet对象
            this.hospset = response.data.item
            console.log(hospset)
        })
    },

    //调用更新的接口
    updateData(){
      this.saveBtnDisabled = true
      hospset.updateById(this.hospset).then(response=>{
          return this.$message({type:'success',message:'修改成功'})
      }).then(response=>{
          //通过路由打开list页面
          this.$router.push({
              path:'/hospSet/list'
          })
      })
},
saveOrUpdate() {
    this.saveBtnDisabled = true
    if (!this.hospset.id) {
        this.saveData()
    } else {
        this.updateData()
    }
},
  }
}
</script>